package cn.com.controller;

import cn.com.pojo.Bill;
import cn.com.pojo.Menu;
import cn.com.pojo.Merchant;
import cn.com.pojo.User;
import cn.com.service.BillService;
import cn.com.service.MenuService;
import cn.com.service.MerchantService;
import cn.com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class AJAXController {
    @Autowired
    UserService service;
    @RequestMapping("/queryCount")
    public User queryCount(String name){
        return service.queryCount(name);
    }
    @Autowired
    MerchantService merchantService;
    @RequestMapping( "/showAllMerchant")
    public List<Merchant> showAll(Merchant merchant){
        if (merchant.getFlag().equals(""))
            merchant.setFlag(null);
        return merchantService.showAll(merchant);
    }
    @RequestMapping( "/showAllUser")
    public List<User> showAll(User user){
        return service.showAll(user);
    }
    @RequestMapping( "/pass")
    public void pass(String user_name){
         merchantService.pass(user_name);
         service.pass(user_name);
    }
    @RequestMapping( "/fail")
    public void fail(String user_name){
         merchantService.fail(user_name);
    }
    @RequestMapping( "/ban")
    public void ban(String user_name){
        merchantService.ban(user_name);
    }
    @RequestMapping( "/unBan")
    public void unBan(String user_name){
        merchantService.unBan(user_name);
    }
    @Autowired
    MenuService menuService;
    @RequestMapping("/showAllMenu")
    public List<Menu> showAllMenu(String userName){
       return menuService.findAllMenuByMerchantName(userName);
    }
    @RequestMapping("/searchMenu")
    public List<Menu> searchMenu(Menu menu,String p){
        if (p!=null &&!p.equals("")) menu.setPrice(Double.parseDouble(p));
        return menuService.search(menu);
    }
    @RequestMapping("/del")
    public void del(String id){
         menuService.del(Integer.parseInt(id));
    }
    @RequestMapping("/findOne")
    public Menu findOne(String id){
        return menuService.findOne(Integer.parseInt(id));
    }
    @RequestMapping("/revise")
    public void revise(Menu menu){
         menuService.revise(menu);
    }
    @Autowired
    BillService billService;
    @RequestMapping("/addBill")
    public void addBill(Bill bill,String p){
        bill.setStatus(0);
        bill.setPrice(Double.parseDouble(p));
      billService.addBill(bill);
    }
    @RequestMapping("/pay")
    public String pay(String shopUserName,String userName,String total,HttpSession session) {
        User user = service.queryCount(userName);
        User shop = service.queryCount(shopUserName);

        if (Double.parseDouble(user.getAccount()) >= Double.parseDouble(total)) {
            billService.update(shopUserName, userName);
            Double r1=Double.parseDouble(user.getAccount())-Double.parseDouble(total);
            Double r2=Double.parseDouble(shop.getAccount())+Double.parseDouble(total);
            service.add(r1,userName);
            service.shop(r2,shopUserName);
            User USER = service.queryCount(userName);
            session.setAttribute("USER",USER);
        }else {
            return "余额不足，请充值！！！";
        }
        return "订单支付完成！！！";
    }
    @RequestMapping("/delBill")
    public void delBill(String id){
        billService.delBill(Integer.parseInt(id));
    }
    @RequestMapping("/showPayBill")
    public List<Bill> showPayBill(String shopUserName,String userName){
      return   billService.findBill(shopUserName,userName,1);
    }
    @RequestMapping("/showNoPayBill")
    public List<Bill>  showNoPayBill(String shopUserName,String userName){
       return billService.findBill(shopUserName,userName,0);
    }
    @RequestMapping("/showAllBill")
    public List<Bill> showAllBill(Bill bill,String p){
        if (p!=null &&!p.equals(""))
            bill.setPrice(Double.parseDouble(p));
        return billService.searchBill(bill);
    }
    @RequestMapping("/addM")
    public String addM(String userName,String total,HttpSession session){
        User user= service.queryCount(userName);
        Double re=Double.parseDouble(user.getAccount())+Double.parseDouble(total);
        service.addM(re,userName);
        User user1= service.queryCount(userName);
        session.setAttribute("USER",user1);
        return "充值成功！！！";
    }
}
